
package com.gitee.flow.client;

import com.gitee.jmash.common.grpc.client.GrpcChannel;
import io.grpc.ManagedChannel;
import jakarta.enterprise.context.ApplicationScoped;
import jakarta.enterprise.inject.Produces;
import jakarta.inject.Singleton;
import jmash.flow.FlowGrpc;

/** flow Client Config . */
@ApplicationScoped
public class FlowClientConfig {

  protected static ManagedChannel channel = null;

  public static ManagedChannel getManagedChannel() {
    if (null != channel && !channel.isShutdown() && !channel.isTerminated()) {
      return channel;
    }
    // k8s环境获取后端服务,本地环境获取测试服务.
    channel = GrpcChannel.getServiceChannel("jmash-flow-service.jmash.svc.cluster.local");
    return channel;
  }

  /** FlowBlockingStub. */
  @Produces
  @Singleton
  public static FlowGrpc.FlowBlockingStub getFlowBlockingStub() {
    return FlowGrpc.newBlockingStub(getManagedChannel());
  }

  /** FlowFutureStub. */
  @Produces
  @Singleton
  public static FlowGrpc.FlowFutureStub getFlowFutureStub() {
    return FlowGrpc.newFutureStub(getManagedChannel());
  }

  /** FlowStub. */
  @Produces
  @Singleton
  public static FlowGrpc.FlowStub getFlowStub() {
    return FlowGrpc.newStub(getManagedChannel());
  }
}
